package com.aipartner.controller;

import com.aipartner.common.Result;
import com.aipartner.util.UserContext;
import com.aipartner.dto.ExampleChatRequest;
import com.aipartner.dto.ExampleDetailResponse;
import com.aipartner.entity.AiImageExampleChat;
import com.aipartner.service.AiImageExampleChatService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.Map;

/**
 * 示例图聊天控制器
 * 
 * @author AI Partner Team
 * @since 2024-01-20
 */
@Slf4j
@RestController
@RequestMapping("/api/ai-image/examples")
@RequiredArgsConstructor
@Validated
public class AiImageExampleChatController {
    
    private final AiImageExampleChatService chatService;
    
    /**
     * 获取示例图详情（包含聊天记录）
     */
    @GetMapping("/{id}/detail")
    public Result<ExampleDetailResponse> getExampleDetail(
            @PathVariable String id,
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "20") @Min(1) Integer size) {
        
        try {
            Long exampleId = Long.parseLong(id);
            Long currentUserId = UserContext.getCurrentUserId();
            ExampleDetailResponse response = chatService.getExampleDetail(exampleId, page, size, currentUserId);
            return Result.success(response);
        } catch (NumberFormatException e) {
            return Result.fail("无效的示例ID格式");
        }
    }
    
    /**
     * 分页获取示例图聊天记录
     */
    @GetMapping("/{id}/chat")
    public Result<IPage<AiImageExampleChat>> getChatPage(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "20") @Min(1) Integer size) {
        
        Long currentUserId = UserContext.getCurrentUserId();
        IPage<AiImageExampleChat> chatPage = chatService.getChatPage(id, page, size, currentUserId);
        return Result.success(chatPage);
    }
    
    /**
     * 发布聊天记录
     */
    @PostMapping("/{id}/chat")
    public Result<AiImageExampleChat> publishChat(
            @PathVariable Long id,
            @Valid @RequestBody ExampleChatRequest request) {
        
        Long userId = UserContext.getCurrentUserId();
        if (userId == null) {
            return Result.fail("请先登录");
        }
        
        AiImageExampleChat chat = chatService.publishChat(id, request, userId);
        return Result.success(chat);
    }
    
    /**
     * 点赞/取消点赞
     */
    @PostMapping("/chat/{id}/like")
    public Result<Map<String, Object>> toggleLike(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();
        if (userId == null) {
            return Result.fail("请先登录");
        }
        
        Boolean liked = chatService.toggleLike(id, userId);
        Map<String, Object> likeInfo = chatService.getLikeInfo(id, userId);
        
        return Result.success(likeInfo);
    }
    
    /**
     * 获取点赞信息
     */
    @GetMapping("/chat/{id}/like")
    public Result<Map<String, Object>> getLikeInfo(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();
        Map<String, Object> likeInfo = chatService.getLikeInfo(id, userId);
        return Result.success(likeInfo);
    }
}
